package com.ruoyi.dev.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class QueryVo {
    public String sql;
    public List<String> wheres = new ArrayList<>();
    public Map<String, Object> params = new HashMap<>();

    public static QueryVo createByTable(String table){
        QueryVo vo = new QueryVo();
        vo.sql = "SELECT * FROM " + table;
        return vo;
    }

    public static QueryVo createBySql(String sql){
        QueryVo vo = new QueryVo();
        vo.sql = sql;
        return vo;
    }

    public void put(String param, Object value){
        params.put(param, value);
    }

    public void where(String param, String exp, Object value){
        if(value == null){
            if(exp.equalsIgnoreCase(DbWhere.EXP_NOT)){
                wheres.add(param+ " is not null");
            }else if(exp.equalsIgnoreCase(DbWhere.EXP_NULL)){
                wheres.add(param+ " is null");
            }
        }else if(exp.equals(DbWhere.EXP_NEQ)){
            wheres.add(param+ " != @"+param);
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_GT)){
            wheres.add(param+ " > @"+param);
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_GTE)){
            wheres.add(param+ " >= @"+param);
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_LT)){
            wheres.add(param+ " < @"+param);
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_LTE)){
            wheres.add(param+ " <= @"+param);
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_LK)){
            wheres.add(param+ " like concat('%', @"+param+" , '%')");
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_NLK)){
            wheres.add(param+ " not like concat('%', @"+param+" , '%')");
            params.put(param, value);
        }else if(exp.equals(DbWhere.EXP_IN)){
            wheres.add(param+ " in ('"+value.toString().replaceAll(",", "','")+"')");
        }else if(exp.equals(DbWhere.EXP_BT)){
            String[] vs = value.toString().split(",");
            wheres.add(param+ " between '" + vs[0] +"'AND '"+vs[1]+"'");
        }else{ // if(exp.equals(DbWhere.EXP_EQ))
            wheres.add(param+ " = @"+param);
            params.put(param, value);
        }
    }

    public void eq(String param, Object value){
        wheres.add(param+ " = @"+param);
        params.put(param, value);
    }

    public void neq(String param, Object value){
        wheres.add(param+ " != @"+param);
        params.put(param, value);
    }

    public void lt(String param, Object value){
        wheres.add(param+ " < @"+param);
        params.put(param, value);
    }

    public void lte(String param, Object value){
        wheres.add(param+ " <= @"+param);
        params.put(param, value);
    }

    public void gt(String param, Object value){
        wheres.add(param+ " > @"+param);
        params.put(param, value);
    }

    public void gte(String param, Object value){
        wheres.add(param+ " >= @"+param);
        params.put(param, value);
    }

    public void lk(String param, Object value){
        wheres.add(param+ " like concat('%', @"+param+" , '%')");
        params.put(param, value);
    }

    public void nlk(String param, Object value){
        wheres.add(param+ " not like concat('%', @"+param+" , '%')");
        params.put(param, value);
    }

    public void nl(String param){
        wheres.add(param+ " is null");
    }

    public void nn(String param){
        wheres.add(param+ " is not null");
    }

    public void in(String param, Object value){
        wheres.add(param+ " in ('"+value.toString().replaceAll(",", "','")+"')");
    }

    public void notin(String param, Object value){
        wheres.add(param+ " not in ('"+value.toString().replaceAll(",", "','")+"')");
    }

    public void bt(String param, Object value){
        String[] vs = value.toString().split(",");
        wheres.add(param+ " between '" + vs[0] +"'AND '"+vs[1]+"'");
    }

    public String getSql(){
        if(StringUtils.isEmpty(wheres)){
            return sql;
        }else{
            return sql + " WHERE " + CollectionUtil.join(wheres, " AND ");
        }
    }

    public void clear() {
        wheres.clear();
        params.clear();
    }
    @Override
    public String toString(){
        return getSql();
    }
}
